package com.my.javaweb.list;

import lombok.Data;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求燃烧图数据
 * @author Felix
 * @date 2023/10/20 20:29
 */
public class DateList {
    @Data
    static class ReqData {
        LocalDate date;
        Long total = 0L;
        Long complete = 0L;
        Long residue = 0L;

        public ReqData(LocalDate date, Long total) {
            this.date = date;
            this.total = total;
        }

        public ReqData(LocalDate date, Long total, Long complete, Long residue) {
            this.date = date;
            this.total = total;
            this.complete = complete;
            this.residue = residue;
        }
    }

    @Data
    static class CompleteData {
        LocalDate date;
        Long complete = 0L;

        public CompleteData(LocalDate date, Long complete) {
            this.date = date;
            this.complete = complete;
        }
    }

    //核心计算逻辑
    static List<ReqData> initList(LocalDate startDate, LocalDate endDate, Long total, List<CompleteData> completeData) {
        List<ReqData> resultList = new ArrayList<>();

        while (!startDate.isAfter(endDate)) {
            resultList.add(new ReqData(startDate, total));
            startDate = startDate.plusDays(1);
        }

        Map<LocalDate, CompleteData> completeDataMap = new HashMap<>();
        for (CompleteData data : completeData) {
            completeDataMap.put(data.date, data);
        }

        Long addUp = 0L;
        for (ReqData data : resultList) {
            CompleteData c = completeDataMap.get(data.date);
            if (c != null) {
                addUp += c.complete;
                data.complete = c.complete;
            }
            data.residue = data.total - addUp;
        }
        return resultList;
    }

    public static void main(String[] args) {
        LocalDate startDate = LocalDate.parse("2023-09-15", DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate endDate = LocalDate.parse("2023-10-03", DateTimeFormatter.ISO_LOCAL_DATE);

        List<CompleteData> completeData = new ArrayList<>();
        completeData.add(new CompleteData(LocalDate.parse("2023-09-19", DateTimeFormatter.ISO_LOCAL_DATE), 1L));
        completeData.add(new CompleteData(LocalDate.parse("2023-09-23", DateTimeFormatter.ISO_LOCAL_DATE), 5L));
        completeData.add(new CompleteData(LocalDate.parse("2023-09-25", DateTimeFormatter.ISO_LOCAL_DATE), 2L));
        long start = System.nanoTime();
        List<ReqData> reqData = initList(startDate, endDate, 10L, completeData);
        System.out.println("填充数据耗时：" + (System.nanoTime() - start)/1000 + " μs");
        for (ReqData r : reqData) {
            System.out.println(r);
        }
    }
}
//怎样将targetDate根据日期对应填充到dateList中